package com.chuangke.file.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public enum FileType {
	
	//Image
	jpg("jpg","image/jpeg"),
	png("png","image/png"),
	gif("gif","image/gif"),
	webp("webp","image/webp"),
	cr2("cr2","image/x-canon-cr2"),
	tif("tif","image/tiff"),
	bmp("bmp","image/bmp"),
	heif("heif","image/heif"),
	jxr("jxr","image/vnd.ms-photo"),
	psd("psd","image/vnd.adobe.photoshop"),
	ico("ico","image/vnd.microsoft.icon"),
	dwg("dwg","image/vnd.dwg"),
	
	//Video
	mp4("mp4","video/mp4"),
	m4v("m4v","video/x-m4v"),
	mkv("mkv","video/x-matroska"),
	webm("webm","video/webm"),
	mov("mov","video/quicktime"),
	avi("avi","video/x-msvideo"),
	wmv("wmv","video/x-ms-wmv"),
	mpg("mpg","video/mpeg"),
	flv("flv","video/x-flv"),
	threegp("3gp","video/3gpp"),
	
	//Audio
	mid("mid","audio/midi"),
	mp3("mp3","audio/mpeg"),
	m4a("m4a","audio/m4a"),
	ogg("ogg","audio/ogg"),
	flac("flac","audio/x-flac"),
	wav("wav","audio/x-wav"),
	amr("amr","audio/amr"),
	aac("aac","audio/aac"),
	
	//Archive
	epub("epub","application/epub+zip"),
	zip("zip","application/zip"),
	tar("tar","application/x-tar"),
	rar("rar","application/vnd.rar"),
	gz("gz","application/gzip"),
	bz2("bz2","application/x-bzip2"),
	sevenz("7z","application/x-7z-compressed"),
	xz("xz","application/x-xz"),
	pdf("pdf","application/pdf"),
	exe("exe","application/vnd.microsoft.portable-executable"),
	swf("swf","application/x-shockwave-flash"),
	rtf("rtf","application/rtf"),
	iso("iso","application/x-iso9660-image"),
	eot("eot","application/octet-stream"),
	ps("ps","application/postscript"),
	sqlite("sqlite","application/vnd.sqlite3"),
	nes("nes","application/x-nintendo-nes-rom"),
	crx("crx","application/x-google-chrome-extension"),
	cab("cab","application/vnd.ms-cab-compressed"),
	deb("deb","application/vnd.debian.binary-package"),
	ar("ar","application/x-unix-archive"),
	Z("Z","application/x-compress"),
	lz("lz","application/x-lzip"),
	rpm("rpm","application/x-rpm"),
	elf("elf","application/x-executable"),
	dcm("dcm","application/dicom"),
	
	//Documents
	doc("doc","application/msword"),
	docx("docx","application/vnd.openxmlformats-officedocument.wordprocessingml.document"),
	xls("xls","application/vnd.ms-excel"),
	xlsx("xlsx","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"),
	ppt("ppt","application/vnd.ms-powerpoint"),
	pptx("pptx","application/vnd.openxmlformats-officedocument.presentationml.presentation"),
	txt("txt","text/plain"),
	
	//Font
	woff("woff","application/font-woff"),
	woff2("woff2","application/font-woff"),
	ttf("ttf","application/font-sfnt"),
	otf("otf","application/font-sfnt");
	
	public static final String[] DOC_MIME_TYPE= new String[] {doc.getMime(),docx.getMime(),xls.getMime(),xlsx.getMime(),
			ppt.getMime(),pptx.getMime(),txt.getMime()} ;
	public static final String[] IMG_MIME_TYPE= new String[] {jpg.getMime(),png.getMime(),gif.getMime(),webp.getMime(),
			cr2.getMime(),tif.getMime(),bmp.getMime(),heif.getMime(),
			jxr.getMime(),psd.getMime(),ico.getMime(),dwg.getMime()} ;
	
	public static final Map<String,String[]> DOC_MAP = new HashMap<>() ;
	static {
		DOC_MAP.put("PDF", new String[] {pdf.getName()}) ;
		DOC_MAP.put("DOC", new String[] {doc.getName(),docx.getName()}) ;
		DOC_MAP.put("PPT", new String[] {ppt.getName(),pptx.getName()}) ;
		DOC_MAP.put("XLS", new String[] {xls.getName(),xlsx.getName()}) ;
		DOC_MAP.put("TXT", new String[] {txt.getName()}) ;
	}
	
	FileType(String name, String mimeType){
		this.name = name ;
		this.mime = mimeType;
	}
	
	private String name;
	private String mime ;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getMime() {
		return mime;
	}

	public void setMime(String mime) {
		this.mime = mime;
	}
	
	public static List<String> getTypes(){
		FileType[] ary = FileType.values();
		List<String> list = new ArrayList<>();
        for (FileType fileType : ary) {
            list.add(fileType.getName());
        }
		return list;
	}
	
	public static Map<String, Map<String, Object>> toMap() {
		FileType[] ary = FileType.values();
		Map<String, Map<String, Object>> enumMap = new HashMap<>();
        for (FileType fileType : ary) {
            Map<String, Object> map = new HashMap<>();
            String key = fileType.getName();
            map.put("name", fileType.getName());
            map.put("mime", fileType.getMime());
            enumMap.put(key, map);
        }
		return enumMap;
	}

	public static List<Map<String, String>> toList() {
		FileType[] ary = FileType.values();
		List<Map<String, String>> list = new ArrayList<>();
        for (FileType fileType : ary) {
            Map<String, String> map = new HashMap<>();
            map.put("name", fileType.getName());
            map.put("mime", fileType.getMime());
            list.add(map);
        }
		return list;
	}
	
    public static FileType getEnum(String code) {
        FileType[] arry = FileType.values();
        for (FileType fileType : arry) {
            if (fileType.getName().equalsIgnoreCase(code)) {
                return fileType;
            }
        }
        return null;
    }

	/**
	 * 取枚举的json字符串
	 * 
	 * @return
	 */
	public static String getJsonStr() {
		FileType[] enums = FileType.values();
		StringBuilder jsonStr = new StringBuilder("[");
		for (FileType senum : enums) {
			if (!"[".equals(jsonStr.toString())) {
				jsonStr.append(",");
			}
			jsonStr.append("{name:'").append(senum.getName()).append("',mime:'").append(senum.getMime()).append("'}");
		}
		jsonStr.append("]");
		return jsonStr.toString();
	}
}
